package com.cykj.util;


import com.cykj.pojo.PsMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * @BeLongsProject: psychologically
 * @BeLongsPackage: com.cykj.utils
 * @Author: 小李
 * @Date: 2025/2/6 01:04
 * @Description: 通用树形菜单utils
 * @Version: 1.0
 */
public class TreeMenu {
    private TreeMenu(){

    }

    //构建树形菜单
    public static List<PsMenu> treeMenuList(List<PsMenu> allMenu){
        //数据库获取所有菜单信息
        List<PsMenu> treeMenu = new ArrayList<>();

        //查找一级菜单
        for (PsMenu data : allMenu) {
            if (data.getMenuParentId() == 0) {
                //一级菜单
                treeMenu.add(findChildren(data, allMenu));
            }
        }
        return treeMenu;
    }

    public static PsMenu findChildren(PsMenu nowMenu, List<PsMenu> data) {
        for (PsMenu datum : data) {
            if (datum.getMenuParentId() == nowMenu.getMenuId()) {
                nowMenu.getChildren().add(findChildren(datum, data));
            }
        }
        return nowMenu;
    }
}
